/// @tags:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>

namespace BlueQuantum {

int const N = 1e6 + 200005;

template <typename T>
inline T &read(T &x) {
  x = 0;
  bool f = false;
  short ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = true;
    ch = getchar();
  }
  while (isdigit(ch)) x = x * 10 + (ch ^ '0'), ch = getchar();
  if (f) x = -x;
  return x;
}

struct Edge {
  int v, nxt;
} e[N];

int n, m, e_cnt;
int heads[N], opt[N], a[N], b[N], ans[N], rsiz[N << 1];
int id[N], fa[N << 1], siz[N << 1], tot;

inline void add(int u, int v) { e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt; }

int find(int x) { return !fa[x] ? x : find(fa[x]); }

void solve(int u) {
  if (!u)
    for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
  if (opt[u] == 1) {
    if (id[a[u]] != -1 && id[b[u]] != -1) {
      int faa = find(id[a[u]]), fab = find(id[b[u]]);
      if (siz[faa] > siz[fab]) std::swap(faa, fab);
      if (faa != fab) { fa[faa] = fab, siz[fab] += siz[faa], rsiz[fab] += rsiz[faa]; }
      for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
      if (faa != fab) { siz[fab] -= siz[faa], rsiz[fab] -= rsiz[faa], fa[faa] = 0; }
    } else
      for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
  }
  if (opt[u] == 2) {
    if (id[a[u]] == -1) ans[u] = -1;
    else
      ans[u] = rsiz[find(id[a[u]])];
    for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
  }
  if (opt[u] == 3) {
    if (id[a[u]] == -1 || id[b[u]] == -1) ans[u] = -1;
    else
      ans[u] = find(id[a[u]]) == find(id[b[u]]);
    for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
  }
  if (opt[u] == 4) {
    if (id[a[u]] == -1) {
      for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
    } else {
      int tmp = id[a[u]], fa = find(tmp);
      rsiz[fa]--, id[a[u]] = -1;
      for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
      rsiz[fa]++, id[a[u]] = tmp;
    }
  }
  if (opt[u] == 5) {
    if (id[a[u]] == -1 || id[b[u]] == -1)
      for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
    else {
      int faa = find(id[a[u]]), fab = find(id[b[u]]), x = ++tot, tmp = id[a[u]];
      rsiz[faa]--, rsiz[fab]++, siz[fab]++, id[a[u]] = x, fa[x] = fab;
      for (int i = heads[u]; i; i = e[i].nxt) solve(e[i].v);
      rsiz[faa]++, rsiz[fab]--, siz[fab]--, id[a[u]] = tmp, fa[x] = 0, tot--;
    }
  }
}

inline int main() {
  read(n), read(m);
  for (int i = 1; i <= n; i++) siz[i] = rsiz[i] = 1, id[i] = i;
  tot = n + 1;
  for (int i = 1, k; i <= m; i++) {
    read(opt[i]), read(k), read(a[i]);
    add(k, i);
    if (opt[i] & 1) read(b[i]);
  }
  solve(0);
  for (int i = 1; i <= m; i++) {
    switch (opt[i]) {
      case 2:
        if (ans[i] == -1) puts("Ywy");
        else
          printf("%d\n", ans[i]);
        break;
      case 3:
        if (ans[i] == -1) puts("Ywy");
        else {
          if (ans[i]) puts("Yes");
          else
            puts("No");
        }
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("unionset.in", "r", stdin);
  freopen("unionset.out", "w", stdout);
#endif
#endif

  return BlueQuantum::main();
}